<?php

class Service_ImageModel {

    const IMG_SERVER = 'http://103.235.253.181:4869/upload'; //图片服务器请求URL

    protected $localImgPath = ''; //本地上传图片存储 路径

    public function __construct ()
    {
        $this->localImgPath = Yaf_Registry::get ( 'config' )->application->tmpdir . '/';
    }

    //文件上传, 参数$field:  前台传入的字段名
    //size 图片大小   0*0 代表原图
    public function fileUpload ($field , $size = '_0*0')
    {
        var_dump($_FILES[$field]);exit();
        $loadObj    = new Upload();
        $fileStatus = $loadObj->upload ( $field );
        if ( $fileStatus )
        {
            $fileNameArr = $loadObj->getFileName ();
            $imgUrls     = array ();
            if ( is_array ( $fileNameArr ) && count ( $fileNameArr ) > 0 )
            {
                foreach ( $fileNameArr as $file )
                {
                    $filePath = $this->localImgPath . $file;
                    $url      = self::imgStorage ( $filePath );
                    unlink ( $filePath ); //删除本地图片
                    if ( $url )
                    {
                        $imgUrls = 'http://103.235.253.181:4869/' . $url . $size;
                    }
                    else
                    {
                        return 2; //图片上传到图片服务器失败
                    }
                }
                return $imgUrls;
            }
            elseif ( $fileNameArr )
            {
                $filePath = $this->localImgPath . $fileNameArr;
                $url      = self::imgStorage ( $filePath );
                unlink ( $filePath ); //删除本地图片
                if ( $url )
                {
                    $imgUrls = 'http://103.235.253.181:4869/' . $url . '_0*0';
                }
                else
                {
                    return 3; //图片上传到图片服务器失败
                }
            }
            return $imgUrls;
        }
        return 1; //上传失败
    }

    /**
     * 保存图片至服务器
     * @param string $file 图片文件路径
     * @return mixed 返回完成后的路径
     */
    static public function imgStorage ($file)
    {
        $cfile  = curl_file_create ( $file , 'image/jpeg' , 'test_name.jpeg' );
        // 设置 POST 数据
        $data   = array ( 'test_file' => $cfile );
        $ch     = curl_init ();
        curl_setopt ( $ch , CURLOPT_URL , self::IMG_SERVER );
        curl_setopt ( $ch , CURLOPT_POST , true );
        curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , true ); //成功返回true，输出内容
        curl_setopt ( $ch , CURLOPT_INFILESIZE , filesize ( $file ) );
        curl_setopt ( $ch , CURLOPT_HTTPHEADER , array ( 'Content-type:jpeg' ) );
        curl_setopt ( $ch , CURLOPT_HTTPHEADER , array ( 'Content-Disposition:attachment' ) );
        curl_setopt ( $ch , CURLOPT_BINARYTRANSFER , 1 );
        curl_setopt ( $ch , CURLOPT_POSTFIELDS , $data );
        $ret    = curl_exec ( $ch );
        if ( ! $ret ) return false; //执行失败
        curl_close ( $ch );
        $search = '/<h1>MD5: (.*?)<\/h1>/si';
        preg_match_all ( $search , $ret , $r );
        return $r[ 1 ][ 0 ];
    }

}
